package com.kyub.buganville

import grails.plugins.springsecurity.Secured


@Secured(['ROLE_ADMIN', 'ROLE_DEVELOPER'])
class IssueController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [issueInstanceList: Issue.list(params), issueInstanceTotal: Issue.count()]
    }

    def create = {
        def issueInstance = new Issue()
        issueInstance.properties = params
        return [issueInstance: issueInstance]
    }

    def save = {
        def Issue issueInstance = new Issue(params)
        String username = principal.username
        def boundUser = User.findByUsername(username)
        issueInstance.opener = boundUser
        if (issueInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'issue.label', default: 'Issue'), issueInstance.id])}"
            redirect(action: "show", id: issueInstance.id)
        }
        else {
            render(view: "create", model: [issueInstance: issueInstance])
        }
    }

    def show = {
        def issueInstance = Issue.get(params.id)
        if (!issueInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'issue.label', default: 'Issue'), params.id])}"
            redirect(action: "list")
        }
        else {
            [issueInstance: issueInstance]
        }
    }

    def edit = {
        def issueInstance = Issue.get(params.id)
        if (!issueInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'issue.label', default: 'Issue'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [issueInstance: issueInstance]
        }
    }

    def update = {
        def issueInstance = Issue.get(params.id)
        if (issueInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (issueInstance.version > version) {
                    
                    issueInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'issue.label', default: 'Issue')] as Object[], "Another user has updated this Issue while you were editing")
                    render(view: "edit", model: [issueInstance: issueInstance])
                    return
                }
            }
            issueInstance.properties = params
            if (!issueInstance.hasErrors() && issueInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'issue.label', default: 'Issue'), issueInstance.id])}"
                redirect(action: "show", id: issueInstance.id)
            }
            else {
                render(view: "edit", model: [issueInstance: issueInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'issue.label', default: 'Issue'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def issueInstance = Issue.get(params.id)
        if (issueInstance) {
            try {
                issueInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'issue.label', default: 'Issue'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'issue.label', default: 'Issue'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'issue.label', default: 'Issue'), params.id])}"
            redirect(action: "list")
        }
    }
}
